// pages/filing/license/license.js
import {
  getUserAgree,
  signAgreement
} from "../../../services/_utils/api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    familyArchiveStatus: '',
    userId: '',
    agreementId: ''
  },
  // 点击同意
  handlePopup() {
    let obj = {
      userId: this.data.userId,
      agreementId: this.data.agreementId
    }
    // 签署协议
    signAgreement(obj).then(res => {
      console.log("签署协议res", res);
      const {
        code,
        msg
      } = res.data
      if (code == 200) {
        console.log(msg);
      }
      if (this.data.familyArchiveStatus) {
        if (this.data.familyArchiveStatus == "empty") {
          wx.navigateTo({
            url: '/pages/filing/filing',
          })
        } else if (this.data.familyArchiveStatus == "member") {
          wx.navigateTo({
            url: '/pages/filing/family/family',
          })
        } else if (this.data.familyArchiveStatus == "health_manage") {
          wx.navigateTo({
            url: '/pages/filing/relationships/relationships',
          })
        } else {
          wx.navigateTo({
            url: '/dataVisualization/home/home',
          })
        }
      } else {
        wx.navigateTo({
          url: '/pages/filing/filing',
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获取userId
    const userId = wx.getStorageSync('userId')
    const {
      currentAgreementVersion,
      familyArchiveStatus
    } = options
    console.log("currentAgreementVersion", currentAgreementVersion);
    // 根据版本号获取用户协议
    getUserAgree({
      // version: currentAgreementVersion
      version: currentAgreementVersion
    }).then(res => {
      console.log("获取用户协议res", res);
      const content = res.data.data?.content
      const agreementId = res.data.data?.agreementId
      this.setData({
        content: content,
        familyArchiveStatus,
        userId,
        agreementId
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})